package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.MerchantApplicationManager;
import service.factory.ManagerFactory;
import util.JsonUtil;
import dto.ResponseDto;

public class UpdateMerchantStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantApplicationManager manager = null;
       
    public UpdateMerchantStatusServlet() {
        super();
        manager = ManagerFactory.getApplicationManagerInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String operation = request.getParameter("operation");
		String rejectReason = request.getParameter("rejectReason");
		ResponseDto responseDto = new ResponseDto();
		PrintWriter writer = response.getWriter();
		responseDto.setCode(-1);
		boolean errorInput = false;
		if (name==null||"".equals(name)) {
			responseDto.setMessage("illegal name");
			errorInput = true;
		}else if(operation==null||"".equals(operation)){
			responseDto.setMessage("illegal opreation");
			errorInput = true;
		}
		if (errorInput) {
			writer.write(JsonUtil.toJsonString(responseDto));
			writer.close();
			return;
		}
		
		String result = manager.updateStatusByName(name, operation);
		if ("SUCCESS".equals(result)) {
			responseDto.setCode(1);
			responseDto.setMessage("success");
		}else{
			responseDto.setMessage(result);
		}
		writer.write(JsonUtil.toJsonString(responseDto));
		writer.close();
		
		
	}

}
